package com.cb.ucenterservice.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.cb.servicebase.exception.GuliException;
import com.cb.ucenterservice.entity.UcenterMember;
import com.cb.ucenterservice.entity.vo.RegisterVo;
import com.cb.ucenterservice.mapper.UcenterMemberMapper;
import com.cb.ucenterservice.service.UcenterMemberService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.cb.utils.JwtUtils;
import com.cb.utils.MD5;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.stereotype.Service;
import org.springframework.util.StringUtils;

/**
 * <p>
 * 会员表 服务实现类
 * </p>
 *
 * @author Cebb
 * @since 2020-06-27
 */
@Service
public class UcenterMemberServiceImpl extends ServiceImpl<UcenterMemberMapper, UcenterMember> implements UcenterMemberService {

    private final RedisTemplate<String, String> redisTemplate;

    @Autowired
    public UcenterMemberServiceImpl(RedisTemplate<String, String> redisTemplate) {
        this.redisTemplate = redisTemplate;
    }

    @Override
    public String login(UcenterMember member) {
        String mobile = member.getMobile();
        String password = member.getPassword();
        if (StringUtils.isEmpty(mobile) || StringUtils.isEmpty(password)) {
            throw new GuliException(20001, "用户名和密码不能为空!");
        }
        QueryWrapper<UcenterMember> wrapper = new QueryWrapper<>();
        wrapper.eq("mobile", mobile);
        UcenterMember ucenterMember = baseMapper.selectOne(wrapper);
        if (ucenterMember == null) {
            throw new GuliException(20001, "用户名不存在!");
        }
        // 将密码通过MD5加密后, 和数据库数据进行对比
        if (!MD5.encrypt(password).equals(ucenterMember.getPassword())) {
            throw new GuliException(20001, "用户名或密码错误!");
        }
        if (ucenterMember.getIsDisabled()) {
            throw new GuliException(20001, "当前账号已被禁用!");
        }
        // 通过用户id和用户昵称, 生成token
        return JwtUtils.getJwtToken(ucenterMember.getId(), ucenterMember.getNickname());
    }

    @Override
    public void register(RegisterVo registerVo) {
        String code = registerVo.getCode();
        String mobile = registerVo.getMobile();
        String password = registerVo.getPassword();
        String nickname = registerVo.getNickname();
        if (StringUtils.isEmpty(code)) {
            throw new GuliException(20001, "验证码不能为空!");
        }
        if (StringUtils.isEmpty(mobile)
                || StringUtils.isEmpty(password)
                || StringUtils.isEmpty(nickname)) {
            throw new GuliException(20001, "用户信息不能为空!");
        }
        // 从redis中取出验证码
        String redisCode = redisTemplate.opsForValue().get(mobile);
        if (!code.equals(redisCode)) {
            throw new GuliException(20001, "验证码错误!");
        }
        // 判断数据库中是否已经存在用户
        QueryWrapper<UcenterMember> wrapper = new QueryWrapper<>();
        wrapper.eq("mobile", mobile).last("limit 1");
        Integer integer = baseMapper.selectCount(wrapper);
        if (integer > 0) {
            throw new GuliException(20001, "当前手机号已被注册!");
        }
        // 验证完成, 加入数据库
        UcenterMember member = new UcenterMember();
        member.setMobile(mobile);
        member.setPassword(MD5.encrypt(password));
        member.setNickname(nickname);
        member.setIsDisabled(false); //不禁用用户
        member.setAvatar("https://guli-edu-cebb.oss-cn-shenzhen.aliyuncs.com/2020/06/22/43d45d18f6f94893a0319bac62ca9384file.png");
        baseMapper.insert(member);
        // 清除redis中的验证码
        redisTemplate.delete(mobile);
    }

    @Override
    public UcenterMember getOpenIdMember(String openid) {
        QueryWrapper<UcenterMember> wrapper = new QueryWrapper<>();
        wrapper.eq("openid", openid);
        UcenterMember member = baseMapper.selectOne(wrapper);
        return member;
    }

    @Override
    public Integer countRegister(String day) {
        return baseMapper.countRegister(day);
    }

}
